package com.zs.usc.utils;

import cn.hutool.http.useragent.Browser;
import cn.hutool.http.useragent.OS;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.alibaba.fastjson.JSONObject;
import com.zs.usc.common.BusinessException;
import org.aspectj.lang.JoinPoint;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Objects;

/**
 * @author Zero
 * @description 解析HttpServletRequest工具类
 * @date 2022/1/7 17:16
 */
public class ReqUtils {

    /**
     * 获取发起请求的IP地址
     */
    public static String getIp(HttpServletRequest request) {
        String ip = null;
        ip = request.getHeader("x-forwarded-for");
        if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
            ip = request.getRemoteAddr();
            if (ip.equals("127.0.0.1")) {
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    throw new BusinessException("获取请求的IP地址失败", e);
                }
                ip = inet.getHostAddress();
            }
        }
        if ((ip != null) && (ip.length() > 15)) {
            if (ip.indexOf(",") > 0) {
                ip = ip.substring(0, ip.indexOf(","));
            }
        }
        return ip;
    }


    public static String getReqParam(JoinPoint joinPoint) {
        // 参数值
        for (Object arg : joinPoint.getArgs()) {
            if (arg instanceof HttpServletRequest) {
            } else if (arg instanceof HttpServletResponse) {
            } else {
                return JSONObject.toJSONString(arg);
            }
        }
        return "未知";
    }

    /**
     * 获取参数列表
     */
    public static String getJson(JoinPoint joinPoint) {
        // 参数值
        for (Object arg : joinPoint.getArgs()) {
            if (arg instanceof HttpServletRequest) {
            } else if (arg instanceof HttpServletResponse) {
            } else {
                return JSONObject.toJSONString(arg);
            }
        }
        return "未知";
    }


    /**
     * 获取发起请求的浏览器名称
     */
    public static String getBrowserName(HttpServletRequest request) {
        String header = request.getHeader("User-Agent");
        UserAgent userAgent = UserAgentUtil.parse(header);
        Browser browser;
        if (Objects.isNull(userAgent) || Objects.isNull(browser = userAgent.getBrowser()))
            return "未知";
        return browser.getName();
    }

    /**
     * 获取发起请求的操作系统名称
     */
    public static String getOsName(HttpServletRequest request) {
        String header = request.getHeader("User-Agent");
        UserAgent userAgent = UserAgentUtil.parse(header);
        OS os;
        if (Objects.isNull(userAgent) || Objects.isNull(os = userAgent.getOs()))
            return "未知";
        return os.getName();
    }


}
